﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BCCommon;
using BCEntity.WeChatComponent.WeChatNewsMaterialArticleRecord;
using Dapper;

namespace BCData.WeChatComponent.WeChatNewsMaterialArticleRecord
{
    /// <summary>
    /// 微信图文素材文章记录数据处理
    /// </summary>
    public class WeChatNewsMaterialArticleRecordData : IWeChatNewsMaterialArticleRecordData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT WeChatNewsMaterialArticleRecordId,Title,Author,ThumbMediaId,
                                            Digest,ShowCoverPic,Content,ContentSourceUrl,MediaId,CreateTime,UpdateTime,
                                            NeedOpenComment,OnlyFansCanComment,`Index`,ThumbUrl,ImageUrl
                                        FROM WeChatNewsMaterialArticleRecord";
        public WeChatNewsMaterialArticleRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public bool Add(WeChatNewsMaterialArticleRecordEntity weChatNewsMaterialArticleRecordEntity)
        {
            string sql = @"INSERT INTO WeChatNewsMaterialArticleRecord(WeChatNewsMaterialArticleRecordId,Title,Author,ThumbMediaId,Digest,Content,ContentSourceUrl,MediaId,NeedOpenComment,OnlyFansCanComment,`Index`,ThumbUrl,ImageUrl)
                                VALUES(UUID(),?Title,?Author,?ThumbMediaId,?Digest,?Content,?ContentSourceUrl,?MediaId,?NeedOpenComment,?OnlyFansCanComment,?Index,?ThumbUrl,?ImageUrl);";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?Title", weChatNewsMaterialArticleRecordEntity.Title, DbType.AnsiString, size: 64);
            parameters.Add("?Author", weChatNewsMaterialArticleRecordEntity.Author, DbType.AnsiString, size: 8);
            parameters.Add("?ThumbMediaId", weChatNewsMaterialArticleRecordEntity.ThumbMediaId, DbType.AnsiString, size: 100);
            parameters.Add("?Digest", weChatNewsMaterialArticleRecordEntity.Digest, DbType.AnsiString, size: 64);
            parameters.Add("?Content", weChatNewsMaterialArticleRecordEntity.Content, DbType.String);
            parameters.Add("?ContentSourceUrl", weChatNewsMaterialArticleRecordEntity.ContentSourceUrl, DbType.AnsiString, size: 1000);
            parameters.Add("?MediaId", weChatNewsMaterialArticleRecordEntity.MediaId, DbType.AnsiString, size: 100);
            parameters.Add("?NeedOpenComment", weChatNewsMaterialArticleRecordEntity.NeedOpenComment, DbType.Int32,size:11);
            parameters.Add("?OnlyFansCanComment", weChatNewsMaterialArticleRecordEntity.OnlyFansCanComment, DbType.Int32, size: 11);
            parameters.Add("?Index", weChatNewsMaterialArticleRecordEntity.Index, DbType.Int32, size: 11);
            parameters.Add("?ThumbUrl", weChatNewsMaterialArticleRecordEntity.ThumbUrl, DbType.AnsiString, size: 1000);
            parameters.Add("?ImageUrl", weChatNewsMaterialArticleRecordEntity.ImageUrl, DbType.AnsiString, size: 1000);
            int result = this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
            return result > 0;
        }

        public IEnumerable<WeChatNewsMaterialArticleRecordEntity> Get(string mediaId)
        {
            string sql = @"{0} WHERE MediaId=?MediaId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MediaId", mediaId, DbType.AnsiString, size: 100);
            return this.databaseContext.Slave.Query<WeChatNewsMaterialArticleRecordEntity>(string.Format(sql, QUERY), parameters);
        }

        public bool Update(WeChatNewsMaterialArticleRecordEntity weChatNewsMaterialArticleRecordEntity)
        {
            string sql = @"UPDATE WeChatNewsMaterialArticleRecord SET 
                                Title=?Title,
                                Author=?Author,
                                ThumbMediaId=?ThumbMediaId,
                                Digest=?Digest,
                                Content=?Content,
                                ContentSourceUrl=?ContentSourceUrl,
                                MediaId=?MediaId,
                                NeedOpenComment=?NeedOpenComment,
                                OnlyFansCanComment=?OnlyFansCanComment,
                                `Index`=?Index,
                                ThumbUrl=?ThumbUrl,
                                ImageUrl=?ImageUrl
                            WHERE WeChatNewsMaterialArticleRecordId=?WeChatNewsMaterialArticleRecordId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WeChatNewsMaterialArticleRecordId", weChatNewsMaterialArticleRecordEntity.WeChatNewsMaterialArticleRecordId, DbType.AnsiString, size: 36);
            parameters.Add("?Title", weChatNewsMaterialArticleRecordEntity.Title, DbType.AnsiString, size: 64);
            parameters.Add("?Author", weChatNewsMaterialArticleRecordEntity.Author, DbType.AnsiString, size: 8);
            parameters.Add("?ThumbMediaId", weChatNewsMaterialArticleRecordEntity.ThumbMediaId, DbType.AnsiString, size: 100);
            parameters.Add("?Digest", weChatNewsMaterialArticleRecordEntity.Digest, DbType.AnsiString, size: 64);
            parameters.Add("?Content", weChatNewsMaterialArticleRecordEntity.Content, DbType.String);
            parameters.Add("?ContentSourceUrl", weChatNewsMaterialArticleRecordEntity.ContentSourceUrl, DbType.AnsiString, size: 1000);
            parameters.Add("?MediaId", weChatNewsMaterialArticleRecordEntity.MediaId, DbType.AnsiString, size: 100);
            parameters.Add("?NeedOpenComment", weChatNewsMaterialArticleRecordEntity.NeedOpenComment, DbType.Int32);
            parameters.Add("?OnlyFansCanComment", weChatNewsMaterialArticleRecordEntity.OnlyFansCanComment, DbType.Int32);
            parameters.Add("?Index", weChatNewsMaterialArticleRecordEntity.Index, DbType.Int32, size: 11);
            parameters.Add("?ThumbUrl", weChatNewsMaterialArticleRecordEntity.ThumbUrl, DbType.AnsiString, size: 1000);
            parameters.Add("?ImageUrl", weChatNewsMaterialArticleRecordEntity.ImageUrl, DbType.AnsiString, size: 1000);
            int result = this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
            return result > 0;
        }

        //public bool UpdateMediaId(string weChatNewsMaterialArticleRecordId, string mediaId)
        //{
        //    string sql = @"UPDATE WeChatNewsMaterialArticleRecord SET 
        //                        MediaId=?MediaId
        //                    WHERE WeChatNewsMaterialArticleRecordId=?WeChatNewsMaterialArticleRecordId;";
        //    DynamicParameters parameters = new DynamicParameters();
        //    parameters.Add("WeChatNewsMaterialArticleRecordId", weChatNewsMaterialArticleRecordId, DbType.AnsiString, size: 36);
        //    parameters.Add("MediaId", mediaId, DbType.AnsiString, size: 100);
        //    int result = this.databaseContext.Slave.Execute(sql, parameters);
        //    return result > 0;
        //}

        public IEnumerable<WeChatNewsMaterialArticleRecordEntity> Get(string title, DateTime? start, DateTime? end, bool? ascending, out int dataCount, int pageIndex = 1, int pageSize = 10)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(title))
            {
                wheres.Add("Title LIKE CONCAT(CONCAT('%',?Title),'%')");
                parameters.Add("?Title", title, DbType.AnsiString, size: 64);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY CreateTime {4} LIMIT {2},{3}; 
                                SELECT COUNT(1) FROM (
                                    SELECT MediaId FROM WeChatNewsMaterialArticleRecord {1}
                                GROUP BY MediaId)A;",
                QUERY,
                wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                pageSize,
                ascending.HasValue && ascending.Value ? "ASC" : "DESC");

            var result = this.databaseContext.Slave.QueryMultiple<WeChatNewsMaterialArticleRecordEntity, int>(sql, parameters);

            dataCount = result.Item2;

            return result.Item1;
        }

        public bool Delete(string mediaId)
        {
            string sql = @"DELETE FROM WeChatNewsMaterialArticleRecord WHERE MediaId=?MediaId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MediaId", mediaId, DbType.AnsiString, size: 100);
            int result = this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
            return result > 0;
        }

        public bool DeleteArticle(string weChatNewsMaterialArticleRecordId)
        {
            string sql = @"DELETE FROM WeChatNewsMaterialArticleRecord WHERE WeChatNewsMaterialArticleRecordId=?WeChatNewsMaterialArticleRecordId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WeChatNewsMaterialArticleRecordId", weChatNewsMaterialArticleRecordId, DbType.AnsiString, size: 36);
            int result = this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
            return result > 0;
        }
    }
}
